using System;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Passageways.Modules.Rolodex.Islands.Rolodex
{
	/// <summary>
	/// Represents the "View Contacts" island instance.
	/// </summary>
	public partial class ContactsMain : Page //TODO: Change to PWIslandContext
	{
		/// <summary>
		/// When the page first loads, we want to fill the DataGrid from the database and bind the DataGrid to that data.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void Page_Load(object sender, EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				BindCategories();
			}
		}

		/// <summary>
		/// Binds the categories to the repeater.
		/// </summary>
		private void BindCategories()
		{
			CategoryCollection categories = Category.Get();

			DataTable categoryTable = new DataTable();
			categoryTable.Columns.Add("ID", typeof(int));
			categoryTable.Columns.Add("Name");

			foreach(Category category in categories)
			{
				//TODO: Get the workgrouID from the page context
				if(Contact.GetNumberInCategory(category.ID, "") > 0)
				{
					DataRow categoryRow = categoryTable.NewRow();
					categoryRow["ID"] = category.ID;
					categoryRow["Name"] = category.Name;

					categoryTable.Rows.Add(categoryRow);
				}
			}

			rptrCategories.DataSource = categoryTable;
			rptrCategories.DataBind();
		}

		/// <summary>
		/// Invoked when an item is bound to the repeater. This method will bind contacts in
		/// a categort to the inner data grids.
		/// </summary>
		protected void OnCategoryDataBound(object sender, RepeaterItemEventArgs e)
		{
			HiddenField hdnCategoryID = (HiddenField)e.Item.FindControl("hdnCategoryID");
			DataGrid dgContacts = (DataGrid)e.Item.FindControl("dgContacts");
			
			int categoryID = int.Parse(hdnCategoryID.Value);

			DataTable contactsTable = new DataTable();
			contactsTable.Columns.Add("CompanyName");
			contactsTable.Columns.Add("ContactName");
			contactsTable.Columns.Add("Phone");
			contactsTable.Columns.Add("Email");
			contactsTable.Columns.Add("Website");
			contactsTable.Columns.Add("Address");
			contactsTable.Columns.Add("Fax");
			contactsTable.Columns.Add("Notes");
			contactsTable.Columns.Add("LastUpdateBy");
			contactsTable.Columns.Add("LastUpdateDateTime", typeof(DateTime));

			//TODO: Get the workgroupID from the page context
			ContactCollection contacts = Contact.Get(categoryID, "");
			
			foreach(Contact contact in contacts)
			{
				StringBuilder address = new StringBuilder();

				if(!string.IsNullOrEmpty(contact.Address.AddressLine1))
				{
					address.Append(contact.Address.AddressLine1 + "<br />");
				}

				if(!string.IsNullOrEmpty(contact.Address.AddressLine2))
				{
					address.Append(contact.Address.AddressLine2 + "<br />");
				}

				if(!string.IsNullOrEmpty(contact.Address.City))
				{
					address.Append(contact.Address.City + ", ");
				}

				if(!string.IsNullOrEmpty(contact.Address.State))
				{
					address.Append(contact.Address.State + " ");
				}

				if(!string.IsNullOrEmpty(contact.Address.Zip))
				{
					address.Append(contact.Address.Zip);
				}

				DataRow contactRow = contactsTable.NewRow();
				contactRow["CompanyName"] = contact.CompanyName;
				contactRow["ContactName"] = contact.Name;
				contactRow["Email"] = contact.Email;
				contactRow["Phone"] = contact.Phone;
				contactRow["Address"] = address;
				contactRow["Fax"] = contact.Fax;
				contactRow["Website"] = contact.Website;
				contactRow["Notes"] = contact.Notes.Replace(Environment.NewLine, "<br />");
				contactRow["LastUpdateBy"] = contact.LastUpdateBy;
				contactRow["LastUpdateDateTime"] = contact.LastUpdateDateTime;
				
				contactsTable.Rows.Add(contactRow);
			}

			dgContacts.DataSource = contactsTable;
			dgContacts.DataBind();
		}
	}
}